﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;

namespace OgilvyOne.BLL
{
    /// <summary>
    /// 模板标签定义基类
    /// </summary>
    abstract class CommonLabel : ICmsLabel
    {
        protected static Regex regex = new Regex("<cms\\s+value=[\'|\"]\\w+[\'|\"]\\s*/>");
        protected CommonModel model;
        public CommonLabel(CommonModel model)
        {
            this.model = model;
        }
        private string methodname;  //方法名
        private string str; //标签字符串
        private string nodestr; //标签内字符串
        

        /// <summary>
        /// 执行方法名
        /// </summary>
        public string MethodName
        {
            set { methodname = value; }
            get { return methodname; }
        }
        /// <summary>
        /// 标签字符串
        /// </summary>
        public string LabelStr
        {
            set { str = value; }
            get { return str; }
        }
        /// <summary>
        /// 标签内字符串
        /// </summary>
        public string LabelNodeStr
        {
            set { nodestr = value; }
            get { return nodestr; }
        }
        /// <summary>
        /// 执行方法的参数
        /// </summary>
        /// <returns></returns>
        public virtual object[] GetMoedthParm()
        {
            return null;
        }

        public abstract string GetLabelHtml();

        protected virtual object Execute()
        {
            if (MethodName == null || MethodName == "") return LabelStr + "<font color='red'>需要MethodName</font>";
            try
            {
                string[] mns = MethodName.Split('.');
                object obj = model;
                foreach (string mn in mns)
                {
                    obj = obj.GetType().InvokeMember(mn, BindingFlags.InvokeMethod | BindingFlags.GetProperty, null, obj, GetMoedthParm());
                    
                    /*MethodInfo method = obj.GetType().GetMethod(mn);
                    if (method != null)
                    {
                        obj = method.Invoke(obj, null);
                    }
                    else
                    {
                        PropertyInfo pi = obj.GetType().GetProperty(mn);
                        obj = pi.GetValue(obj, null);

                        
                    }*/
                }
                return obj;
            }
            catch 
            {
                return "调用<" + MethodName + ">方法出错!";    
            }
        }
        /// <summary>
        /// 获取<cms value="title"/> 中的title
        /// </summary>
        /// <param name="cmsstr"></param>
        /// <returns></returns>
        protected string GetCmsValueStr(string cmsstr)
        {
            return cmsstr.Replace("\"", "").Replace("'", "").Replace("<cms", "").Replace("/", "").Replace(">", "").Replace("value=", "").Trim();
        }
    }
}
